package mygame.model;

import Managers.MesajeManager;
import Managers.ObjectManager;
//import Managers.SoundManager;
import com.bulletphysics.collision.broadphase.CollisionFilterGroups;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingVolume;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CylinderCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.CollisionResults;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.effect.shapes.EmitterSphereShape;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;
import com.jme3.system.AppSettings;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.util.SkyFactory;
import de.lessvoid.nifty.Nifty;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3tools.converters.ImageToAwt;
import mygame.controllers.BombControl;
import mygame.controllers.CoinControll;
import mygame.controllers.GameSettings;
import mygame.controllers.MyCharacterControl;

public class Game extends SimpleApplication implements ActionListener, PhysicsCollisionListener, AnimEventListener{

    private MesajeManager mesajeManager;
//    public SoundManager soundManager;
    private static final String CONF = "conf/game.xml";
    private Nifty gui;
    private int currentLevel=0;
    private boolean endGame=false;
    private GameSettings gameSettings;
//    private float soundVolume, musicVolume;
    private int health = 100;
    private int score;
//  Game Physics state
    private BulletAppState bulletAppState;
//  Game Nodes
// TODO tener cada instancia de cada nodo la verdad es medio desprolijo...
    private Geometry wall;
    private Node player;
    private Node enemy;
    private Node ap1;
    private Node ap2;
    private Node ap3;
    private Node coin10_1;
    private Node coin10_2;
    private Node coin10_3;
    
    //character
    CharacterControl character;
    Node model;
    Node coin;

    //temp vectors
    Vector3f walkDirection = new Vector3f();
    float airTime = 0;
    
    //EFECTOS
    //bullet
    Sphere bullet;
    SphereCollisionShape bulletCollisionShape;
    //explosion
    ParticleEmitter effect;
    
    
    public static void main(String[] args) {
        Logger.getLogger("").setLevel(Level.WARNING);
        Game app = new Game();
        //remove initial screen
        app.setShowSettings(false);
        AppSettings settings=new AppSettings(true);
        settings.put("Width", 640);
        settings.put("Height", 480);
        settings.put("Title", "Kutralko - Arroyo de Fuego");
//        settings.put("VSync", true);
//        settings.put("Samples", 4);
        app.setSettings(settings);

        app.start();
    }
    
    @Override
    public void simpleInitApp(){
        bulletAppState = new BulletAppState();
        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
        stateManager.attach(bulletAppState);
        bulletAppState.getPhysicsSpace().enableDebug(assetManager);

        
        mesajeManager = new MesajeManager( this, guiFont );
//        SoundManager.initSoundManager(this);
        ObjectManager.configure(this, CONF);
        NiftyJmeDisplay guiDisplay = new NiftyJmeDisplay(assetManager, inputManager,audioRenderer,guiViewPort);
        gui = guiDisplay.getNifty();
        
        gameSettings= new GameSettings(this);
        
        gui.fromXml("Interface/screenGUI.xml", "start", gameSettings);
        guiViewPort.addProcessor(guiDisplay);
        flyCam.setDragToRotate(true);
               
        ObjectManager.getInstance().loadLevel(currentLevel);
        mesajeManager.clean();
        
        gameSettings.setHealth(health);
        gameSettings.setScore(score);
        getPhysicsSpace().addCollisionListener(this);
        setupKeys();
        
        // ESCNARIO 1
        /*prepareBullet();*/
        prepareEffect();
        createSky();
        createTerrain();
        
        createWall();
        createCharacter();
        setupChaseCamera();
        setupAnimationController();
        setupFilter();
    }
    
    
    @Override
    public void simpleUpdate(float tpf) {
        
        if( !endGame ){
            //super.update();
            getPhysicsSpace().update(tpf);
            updateWalkDirection();
            calcAirTime(tpf);
            updateAnimationChannel();
            
            character.setWalkDirection(walkDirection);

            //rootNode.updateGeometricState();
            // For 3d audio

            listener.setLocation(cam.getLocation());
            listener.setRotation(cam.getRotation());
            

            /*CollisionResults results = new CollisionResults();
            CollisionResults points = new CollisionResults();
            CollisionResults healthCollision = new CollisionResults();

            
            wall=(Geometry) rootNode.getChild("Target");
            enemy=(Node) rootNode.getChild("Enemy");
            player= model;
            
            ap1=(Node)rootNode.getChild("50Coin1");
            ap2=(Node)rootNode.getChild("50Coin2");
            ap3=(Node)rootNode.getChild("50Coin3");
            
            coin10_1=(Node)rootNode.getChild("10Coin1");
            coin10_2=(Node)rootNode.getChild("10Coin2");
            coin10_3=(Node)rootNode.getChild("10Coin3");
            
            BoundingVolume ap1Volum=ap1.getWorldBound();
            BoundingVolume ap2Volum=ap2.getWorldBound();
            */
            /*player.collideWith(ap1Volum, points);
            if(points.size() > 0){
                score+=10;
                gameSettings.setScore(score);
            }*/
            
            /*player.collideWith(ap2Volum, points);
            if(points.size() > 0){
                score+=20;
                gameSettings.setScore(score);
            }*/
            
            
            /*BoundingVolume ev = enemy.getWorldBound();
            player.collideWith(ev, healthCollision);
            
            if(healthCollision.size()>0){
                if(this.health>0){
                    this.health--;
                }
                gameSettings.setHealth(this.health);
            }*/
            
            /*BoundingVolume bv = wall.getWorldBound();
            player.collideWith(bv, results);

            if(results.size() > 0){
                wall.getMaterial().setColor("Color", ColorRGBA.Red);
                //mesajeManager.showGoalImage("Textures/ColoredTex/Monkey.png");
                currentLevel++;
                System.out.println("PASASTES D NIVEL !!");
                if(ObjectManager.getInstance().loadLevel(currentLevel)){
                    System.out.println("GANASTESS!");
                    endGame=true;
                }

            }else{
                wall.getMaterial().setColor("Color", ColorRGBA.Green);
            }*/ 
        }
    }
    
    private void calcAirTime(float tpf) {
        if (!character.onGround()) {
            airTime = airTime + tpf;
        } else {
            airTime = 0;
        }
    }
    
    private void updateAnimationChannel() {
        if (walkDirection.length() == 0) {
            if (!"stand".equals(animationChannel.getAnimationName())&&
                    !"kick".equals(animationChannel.getAnimationName()) &&
                    !"crouch".equals(animationChannel.getAnimationName()) ) {
                animationChannel.setAnim("stand");
                animationChannel.setSpeed(5f);
            }
        } else {
            character.setViewDirection(walkDirection);
            if (airTime > .3f) {
                 if (!"jump".equals(animationChannel.getAnimationName())) {
                  animationChannel.setAnim("jump",5f);
                  animationChannel.setSpeed(5f);
                }
            }else if (!"walk".equals(animationChannel.getAnimationName())&&
                    !"crouch".equals(animationChannel.getAnimationName())) {
                animationChannel.setAnim("walk",0.7f);
                animationChannel.setSpeed(5f);
            }
        }
    }

    private void updateWalkDirection() {
        // movilidad del personaje
        Vector3f camDir = cam.getDirection().clone().multLocal(0.2f);
        Vector3f camLeft = cam.getLeft().clone().multLocal(0.2f);
        camDir.y = 0;
        camLeft.y = 0;
        
        walkDirection.set(0, 0, 0);
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (up) {
            walkDirection.addLocal(camDir);
        }
        if (down) {
            walkDirection.addLocal(camDir.negate());
        }
    }
    
    @Override
    public void simpleRender(RenderManager rm) {
//        ap1.rotate(0, 0.1f* speed,0);
//        ap2.rotate(0, 0.1f* speed,0);
//        ap3.rotate(0, 0.1f* speed,0);
        
        //coin.rotate(0, 0.1f* speed, 0);
//        coin10_2.rotate(0, 0, 0.1f* speed);
//        coin10_3.rotate(0, 0, 0.1f* speed);
    }
   
    @Override
    public void onAction(String binding, boolean value, float tpf) {
        
        if (binding.equals("CharLeft")) {
            left = value;
        } else if (binding.equals("CharRight")) {
            right = value;
        } else if (binding.equals("CharUp")) {
            up = value;
        } else if (binding.equals("CharDown")) {
            down = value;
        } else if (binding.equals("CharSpace")) {
            character.setCollisionShape(standCapsule);
           if (!"jump".equals(animationChannel.getAnimationName())) {
                animationChannel.setAnim("jump");
                animationChannel.setSpeed(5f);
                animationChannel.setLoopMode(LoopMode.DontLoop);
                character.jump();
            }
        } else if (binding.equals("CharShoot") && !value) {
            character.setCollisionShape(standCapsule);
            if (!"punch".equals(animationChannel.getAnimationName())) {
                animationChannel.setAnim("punch",5f);
                animationChannel.setSpeed(40f);
                animationChannel.setLoopMode(LoopMode.DontLoop);
                //shootBullet();
                /*bulletControl();*/
            }
        }else if (binding.equals("CharKick")) {
            character.setCollisionShape(standCapsule);
            if (!"kick".equals(animationChannel.getAnimationName())) {
                animationChannel.setAnim("kick");
                animationChannel.setSpeed(15f);
                animationChannel.setLoopMode(LoopMode.DontLoop);
            }
        }else if (binding.equals("CharCrouch")) {
            if (!"crouch".equals(animationChannel.getAnimationName())) {
                character.setCollisionShape(crouchCapsule);
                animationChannel.setAnim("crouch");
                animationChannel.setSpeed(15f);
                animationChannel.setLoopMode(LoopMode.DontLoop);
            }
        // TODO ver para que esta esto...
        }else if (binding.equals("monedaA")) {
            coin.setLocalTranslation(0, tpf, 0);
        }else if (binding.equals("monedaA")) {
            coin.setLocalTranslation(0, -tpf, 0);
        }
    }
    
    /*
     private void bulletControl() {
//        shootingChannel.setAnim("Dodge", 0.1f);
//        shootingChannel.setLoopMode(LoopMode.DontLoop);
        Geometry bulletg = new Geometry("bullet", bullet);
        bulletg.setMaterial(matBullet);
        bulletg.setShadowMode(ShadowMode.CastAndReceive);
        bulletg.setLocalTranslation(character.getPhysicsLocation().add(cam.getDirection().mult(2)));
        RigidBodyControl bulletControl = new BombControl(bulletCollisionShape, 1);
        bulletControl.setCcdMotionThreshold(0.1f);
        bulletControl.setLinearVelocity(cam.getDirection().mult(2).add(new Vector3f(0,0.1f,0)));
        bulletg.addControl(bulletControl);
        rootNode.attachChild(bulletg);
        getPhysicsSpace().add(bulletControl);
    }*/
     
    public MesajeManager getMesajeManager() {
        return mesajeManager;
    }
    
    
    // ESCNARIO FISICO 2
    
    public PhysicsSpace getPhysicsSpace() {
        return bulletAppState.getPhysicsSpace();
    }
    
     //Materials
    Material matRock;
    Material matBullet;
    //terrain
    TerrainQuad terrain;
    TerrainQuad terrain2;
    RigidBodyControl terrainPhysicsNode;
    
    private void createTerrain() {
        matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
        matRock.setBoolean("useTriPlanarMapping", false);
        matRock.setBoolean("WardIso", true);
        matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
        Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
        Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
        grass.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap", grass);
        matRock.setFloat("DiffuseMap_0_scale", 64);
        Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
        dirt.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap_1", dirt);
        matRock.setFloat("DiffuseMap_1_scale", 16);
        Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
        rock.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap_2", rock);
        matRock.setFloat("DiffuseMap_2_scale", 128);
        Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
        normalMap0.setWrap(WrapMode.Repeat);
        Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
        normalMap1.setWrap(WrapMode.Repeat);
        Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
        normalMap2.setWrap(WrapMode.Repeat);
        matRock.setTexture("NormalMap", normalMap0);
        matRock.setTexture("NormalMap_1", normalMap2);
        matRock.setTexture("NormalMap_2", normalMap2);

        AbstractHeightMap heightmap = null;
        try {
            heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), false, true, 0), 0.25f);
            heightmap.load();

        } catch (Exception e) {
            e.printStackTrace();
        }

        terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
//        List<Camera> cameras = new ArrayList<Camera>();
//        cameras.add(getCamera());
//        TerrainLodControl control = new TerrainLodControl(terrain, cameras);
//        terrain.addControl(control);
        terrain.setMaterial(matRock);
        terrain.setLocalScale(new Vector3f(2, 2, 2));
        //terrain.setLocalTranslation(0,-2f,0);

       // Node terrain2=(Node) assetManager.loadAsset("Models/newScene.j3o");
        terrainPhysicsNode = new RigidBodyControl(CollisionShapeFactory.createMeshShape(terrain), 0);
        terrain.addControl(terrainPhysicsNode);
        rootNode.attachChild(terrain);
        getPhysicsSpace().add(terrainPhysicsNode);
    }
     
     
    RigidBodyControl coinControl;
    CapsuleCollisionShape standCapsule;
    CapsuleCollisionShape crouchCapsule;
            
    private void createCharacter() {
        standCapsule = new CapsuleCollisionShape(1.5f, 3f);
        crouchCapsule = new CapsuleCollisionShape(1.5f, 1f);
        
        character = new MyCharacterControl(standCapsule, 0.01f);        
        model = (Node) assetManager.loadModel("Models/MergedC/male.j3o");
        model.setName("pelado");
        model.setLocalScale(0.5f);
        model.addControl(character);
        character.setPhysicsLocation(new Vector3f(-140, 15, -10));
        getPhysicsSpace().add(character);
        rootNode.attachChild(model);
               
        // Add a physics box to the world
        coin =(Node) assetManager.loadModel("Models/Coins/50Cent.mesh.j3o");
        coin.setName("moneda");
        //RigidBodyControl control = new RigidBodyControl( new CylinderCollisionShape(new Vector3f(1.5f, 1.5f, 0.2f)), 0);
        //coin.addControl(control);
        coinControl = new CoinControll(new CylinderCollisionShape(new Vector3f(1.5f, 1.5f, 0.2f)), 0f);
        
        coin.addControl(coinControl);
                
//        physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
        coin.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(-140, 12, -5));
        getPhysicsSpace().add(coin);
        rootNode.attachChild(coin);
    }
        
    private void setupKeys() {
        inputManager.addMapping("wireframe", new KeyTrigger(KeyInput.KEY_T));
        inputManager.addListener(this, "wireframe");
        inputManager.addMapping("CharLeft", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("CharRight", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("CharUp", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("CharDown", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("CharSpace", new KeyTrigger(KeyInput.KEY_RETURN));
        inputManager.addMapping("CharShoot", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("CharKick", new KeyTrigger(KeyInput.KEY_K));
        inputManager.addMapping("CharCrouch", new KeyTrigger(KeyInput.KEY_L));
        
        inputManager.addMapping("monedaA", new KeyTrigger(KeyInput.KEY_G));
        inputManager.addMapping("monedaB", new KeyTrigger(KeyInput.KEY_B));
        
        inputManager.addListener(this, "CharLeft");
        inputManager.addListener(this, "CharRight");
        inputManager.addListener(this, "CharUp");
        inputManager.addListener(this, "CharDown");
        inputManager.addListener(this, "CharSpace");
        inputManager.addListener(this, "CharShoot");
        inputManager.addListener(this, "CharCrouch");
        inputManager.addListener(this, "CharKick");
        inputManager.addListener(this, "monedaA");
        inputManager.addListener(this, "monedaB");
    }

     //animation
    AnimChannel animationChannel;
    AnimControl animationControl;
    
    private void setupAnimationController() {
        animationControl = model.getControl(AnimControl.class);
        animationControl.addListener(this);
        animationChannel = animationControl.createChannel();
    
    }
    
    
    /*
    private void prepareBullet() {
        bullet = new Sphere(32, 32, 0.4f, true, false);
        bullet.setTextureMode(TextureMode.Projected);
        bulletCollisionShape = new SphereCollisionShape(0.4f);
        matBullet = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        matBullet.setColor("Color", ColorRGBA.Green);
        matBullet.setColor("m_GlowColor", ColorRGBA.Green);
        getPhysicsSpace().addCollisionListener(this);
    }*/

    private void prepareEffect() {
        int COUNT_FACTOR = 1;
        float COUNT_FACTOR_F = 1f;
        effect = new ParticleEmitter("Flame", Type.Triangle, 32 * COUNT_FACTOR);
        effect.setSelectRandomImage(true);
        effect.setStartColor(new ColorRGBA(1f, 0.4f, 0.05f, (float) (1f / COUNT_FACTOR_F)));
        effect.setEndColor(new ColorRGBA(.4f, .22f, .12f, 0f));
        effect.setStartSize(1.3f);
        effect.setEndSize(2f);
        effect.setShape(new EmitterSphereShape(Vector3f.ZERO, 1f));
        effect.setParticlesPerSec(0);
        effect.setGravity(0, -5, 0);
        effect.setLowLife(.4f);
        effect.setHighLife(.5f);
        effect.setInitialVelocity(new Vector3f(0, 7, 0));
        effect.setVelocityVariation(1f);
        effect.setImagesX(2);
        effect.setImagesY(2);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
        mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
        effect.setMaterial(mat);
        effect.setLocalScale(100);
        rootNode.attachChild(effect);
    }

      
    //SKYBOX
      
    private void createSky() {
        rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
    }
    
    //CAMERA
    boolean left = false, right = false, up = false, down = false;
    ChaseCamera chaseCam;
    
    
     private void setupChaseCamera() {
        flyCam.setEnabled(true);
        chaseCam = new ChaseCamera(cam, model, inputManager);
    }
     
     // FILTER
    // FilterPostProcessor fpp;
     
     private void setupFilter() {
        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
        BloomFilter bloom = new BloomFilter(BloomFilter.GlowMode.Objects);
        fpp.addFilter(bloom);
        viewPort.addProcessor(fpp);
    }

    /*@Override
     public void collision(PhysicsCollisionEvent event) {
         
        if (event.getObjectA() instanceof BombControl) {
            final Spatial node = event.getNodeA();
            effect.killAllParticles();
            effect.setLocalTranslation(node.getLocalTranslation());
            effect.emitAllParticles();
        } else if (event.getObjectB() instanceof BombControl) {
            final Spatial node = event.getNodeB();
            effect.killAllParticles();
            effect.setLocalTranslation(node.getLocalTranslation());
            effect.emitAllParticles();
        } else if (event.getObjectA() instanceof MyCharacterControl 
                && event.getObjectB() instanceof CoinControll) {
            getPhysicsSpace().remove(coinControl);
            event.getNodeB().removeFromParent();
             score+=10;
             gameSettings.setScore(score);
            System.out.println("gana puntoA" );
            System.out.println("hay que sacar la moneda" );
        } else if (event.getObjectA() instanceof CoinControll &&
                event.getObjectB() instanceof MyCharacterControl) {
            getPhysicsSpace().remove(coinControl);
            event.getNodeA().removeFromParent();
             score+=10;
             gameSettings.setScore(score);
            System.out.println("gana puntoB" );
            System.out.println("hay que sacar la moneda" );
        } 
    }*/
    
     
    @Override
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
      
    }

    @Override
    public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
        
    }
    
    
     //brick wall
    Box brick;
    float bLength = 0.8f;
    float bWidth = 0.4f;
    float bHeight = 0.4f;
    
    private void createWall() {
        float xOff = -230;
        float zOff = 18;
        float startpt = bLength / 4 - xOff;
        float height = 6.1f;
        brick = new Box(Vector3f.ZERO, bLength, bHeight, bWidth);
        brick.scaleTextureCoordinates(new Vector2f(1f, .5f));
        for (int j = 0; j < 15; j++) {
            for (int i = 0; i < 4; i++) {
                Vector3f vt = new Vector3f(i * bLength * 2 + startpt, bHeight + height, zOff);
                addBrick(vt);
            }
            startpt = -startpt;
            height += 1.01f * bHeight;
        }
    }

    private void addBrick(Vector3f ori) {
        Geometry reBoxg = new Geometry("brick", brick);
        reBoxg.setMaterial(matRock);
        reBoxg.setLocalTranslation(ori);
        reBoxg.addControl(new RigidBodyControl(1.5f));
        reBoxg.setShadowMode(ShadowMode.CastAndReceive);
        this.rootNode.attachChild(reBoxg);
        this.getPhysicsSpace().add(reBoxg);
    }

    @Override
    public void collision(PhysicsCollisionEvent event) {
        if ( event.getNodeA().getName().equals("pelado") && event.getNodeB().getName().equals("moneda") ) {
            //final Node node = event.getNodeA();
            System.out.println("incremento puntaje A" );
            getPhysicsSpace().remove(coin.getControl(RigidBodyControl.class));
            coin.removeFromParent();
            rootNode.updateGeometricState();
            /** ... do something with the node ... */
        } else if ( event.getNodeA().getName().equals("moneda") && event.getNodeB().getName().equals("pelado")) {
            //final Node node = event.getNodeB();
            /** ... do something with the node ... */
            System.out.println("incremento puntaje B" );
            getPhysicsSpace().remove(coin.getControl(RigidBodyControl.class));
            coin.removeFromParent();
            rootNode.updateGeometricState();
        }
    }

}
